package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.Discover;
import com.innovation.chengzhangriji.DiscoverSaveComment;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.DataResponse;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.http.TypeDataResponse;
import com.innovation.chengzhangriji.ui.adapter.HistoryVideoAdapter;
import com.innovation.chengzhangriji.ui.adapter.HistoryVideoContentHolder;
import com.innovation.chengzhangriji.ui.event.BaseEvent;
import com.innovation.chengzhangriji.ui.model.DiscoverModel;
import com.innovation.chengzhangriji.ui.model.UIHistoryPhoto;
import com.innovation.chengzhangriji.ui.model.UIHistoryPhotoDate;
import com.innovation.chengzhangriji.ui.model.UIHistoryVideo;
import com.innovation.chengzhangriji.ui.model.UIHistoryVideoContent;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;
import com.innovation.chengzhangriji.util.BabyUtil;
import com.innovation.chengzhangriji.util.ChZhRJUtils;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import de.greenrobot.event.EventBus;

public class HistoryVideoActivity extends BaseHistoryPhotoAndVideoActivity implements HistoryVideoAdapter.HistoryVideoAgent, HistoryVideoContentHolder.VideoClickListener, CuteToolbar.OnClickCuteToolbarListener {

    @Override
    public void onVideoClick(String diaryId, String url, String localUrl) {
        Intent intent = new Intent(HistoryVideoActivity.this, TimelinePlayActivity.class);
        intent.putExtra("uuid", diaryId);
        intent.putExtra("url", url);
        intent.putExtra("localUrl", localUrl);
        startActivity(intent);
    }

    @Override
    protected String getCuteTitle() {
        return "小视频";
    }

    // 转换数据格式
    @Override
    protected void setupData() {
        photos.clear();
        // 按时间倒叙排序
        Collections.sort(models, new Comparator<Discover>() {
            @Override
            public int compare(Discover lhs, Discover rhs) {
                return rhs.getCreateTS().compareTo(lhs.getCreateTS());
            }
        });
        Date lastCreateMonth = new Date();
        String lastAge = null;
        List<UIHistoryVideo> historyVideos = null;
        for(int i = 0; i < models.size(); i++) {
            if(models.get(i).getGrowthStageID() == 1) {
                Date date = new Date(models.get(i).getCreateTS());
                if(i == 0 || date.getYear() != lastCreateMonth.getYear() || date.getMonth() != lastCreateMonth.getMonth()) {
                    lastCreateMonth = date;
                    // 添加一个时间轴
                    UIHistoryPhotoDate dateItem = new UIHistoryPhotoDate();
                    dateItem.setDate("备孕中 " + (date.getYear() + 1900) +"年" + (date.getMonth() + 1) + "月");
                    photos.add(dateItem);
                    UIHistoryVideoContent content = new UIHistoryVideoContent();
                    historyVideos = new ArrayList<>();
                    if(historyVideos != null) {
                        content.setVideos(historyVideos);
                    }
                    photos.add(content);
                }
            } else {
                String currentAge = BabyUtil.getBabyDay(mBaby, models.get(i).getGrowthStageID(), models.get(i).getCreateTS());
                if(models.get(i).getGrowthStageID() == 2) {
                    int index = currentAge.indexOf("周");
                    if(index == -1) {
                        currentAge = "孕0周";
                    } else {
                        currentAge = currentAge.substring(0, index + 1);
                    }
                } else {
                    int index = currentAge.indexOf("月");
                    if(index == -1) {
                        String age = "";
                        index = currentAge.indexOf("岁");
                        if(index != -1) {
                            age = currentAge.substring(0, index + 1);
                        }
                        currentAge = age + "0个月";
                    } else {
                        currentAge = currentAge.substring(0, index + 1);
                    }
                }
                if(lastAge == null || !lastAge.equals(currentAge)) {
                    lastAge = currentAge;
                    // 添加一个时间轴
                    UIHistoryPhotoDate dateItem = new UIHistoryPhotoDate();
                    dateItem.setDate(lastAge);
                    photos.add(dateItem);
                    UIHistoryVideoContent content = new UIHistoryVideoContent();
                    historyVideos = new ArrayList<>();
                    if(historyVideos != null) {
                        content.setVideos(historyVideos);
                    }
                    photos.add(content);
                }
            }
            // 添加一个视频
            UIHistoryVideo video = new UIHistoryVideo();
            video.setUrl(models.get(i).getVideo());
            video.setDuration(models.get(i).getDuration());
            video.setDiaryId(models.get(i).getDiaryID());
            video.setLocalUrl(models.get(i).getVideoLocalUrl());
            video.setThumbnailUrl(models.get(i).getScreenshot());
            historyVideos.add(video);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected int getType() {
        return 4;
    }

    @Override
    protected RecyclerView.Adapter<RecyclerView.ViewHolder> getAdapter() {
        return new HistoryVideoAdapter(this);
    }

    @Override
    public HistoryVideoContentHolder.VideoClickListener getVideoClickListener() {
        return this;
    }

    @Override
    public List<UIHistoryPhoto> getItemList() {
        return photos;
    }
}
